package com.nuanshui.heatedloan.msgcenter.msgparams;

import com.alibaba.dubbo.config.annotation.Reference;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.middleware.msgcenter.annotation.MsgParams;
import com.nuanshui.heatedloan.middleware.msgcenter.bean.Model;
import com.nuanshui.heatedloan.middleware.msgcenter.bean.MsgBody;
import com.nuanshui.heatedloan.msgcenter.constant.ClientType;
import com.nuanshui.heatedloan.msgcenter.constant.SmsMsgBizType;
import com.nuanshui.heatedloan.msgcenter.facade.AppPushMsgService;
import com.nuanshui.heatedloan.msgcenter.form.AppPushMsgForm;
import com.nuanshui.heatedloan.msgcenter.integration.AppPushMsgBody;
import com.nuanshui.heatedloan.msgcenter.model.MsgBizType;
import com.nuanshui.heatedloan.msgcenter.vo.AppPushMsgVO;
import com.nuanshui.heatedloan.userinfo.facade.UserQueryService;
import com.nuanshui.heatedloan.userinfo.facade.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;


@Component
public class AppPushParamsParse {

    private static final String HEATEDLOAN_PROTOCOL_PREFIX = "heatedloan://"; //科博会协议的前缀

    @Autowired
    private AppPushMsgService appPushMsgService;

    @Reference(url = GlobalParam.USER_DUBBO_SERVER, timeout = GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.USER_DUBBO_SERVER_VERSION)
    private UserService userService;

    @Reference(url = GlobalParam.USER_DUBBO_SERVER, timeout = GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.USER_DUBBO_SERVER_VERSION)
    private UserQueryService userQueryService;

    /**
     * 系统推送的参数处理
     *
     * @param @param model
     * @author guohao
     * @date 2017年1月16日 下午5:26:37
     * @since $
     */
    @MsgParams(SmsMsgBizType.ADMIN_APP_PUSH)
    public void parse(AppPushMsgBody model) {
        String id = model.getBizId();

        if (StringUtils.isNotEmpty(id)) {
            AppPushMsgForm vo = appPushMsgService.loadCacheMsg(id);

            if(vo.getMsgType()==1)
            {
                model.setFlag(AppPushMsgBody.FLAG_PERSISTENCE);
            }

            model.setMsgTitle(vo.getTitle());
            model.setMsgContent(vo.getContent());
            model.setDirectUrl(vo.getDirectUrl());
            model.setImageId(vo.getImageId());
            model.setEnv(vo.getEnv());
            int ios = vo.getIos() == null ? 0 : vo.getIos();
            int android = vo.getAndroid() == null ? 0 : vo.getAndroid();
            if (ios == 1 && android == 0) {//只推送ios
                model.setClientType(ClientType.IOS);
            }
            if (ios == 0 && android == 1) {//只推送android
                model.setClientType(ClientType.ANDROID);
            }

            if (StringUtils.isNotEmpty(vo.getUserIds())) {
                model.addDstUserIds(Arrays.asList(vo.getUserIds().split(",")));
            } else {//获取所有的用户
                model.addDstUserIds(userQueryService.loadAllUserIds());
            }
            model.setMsgId(vo.getMsgId());

            model.setMessageType(MsgBizType.SYSTEM.getType(), MsgBizType.SYSTEM.getTypeName());

            //解析 填写的jumpurl
            if (StringUtils.isNotEmpty(vo.getDirectUrl())) {
                parseJumpUrl(vo.getDirectUrl(), model);
            }
        }
    }

    private void parseJumpUrl(String url, Model model) {
        if (url.startsWith(HEATEDLOAN_PROTOCOL_PREFIX)) {//如果不是kebohui协议 将不再解析
            url = url.substring(HEATEDLOAN_PROTOCOL_PREFIX.length());
            if (StringUtils.isNotEmpty(url)) {
                String strs[] = url.split("&&");
                for (String str : strs) {
                    int index = str.indexOf("=");
                    if (index < 0) continue;
                    String key = str.substring(0, index);
                    String value = str.substring(index + 1);
                    model.putParam(key, value);
                }
            }
        }
    }

    public static void main(String[] args) {
        System.out.println("heatedloan://actiontype=200&&actionname=DetailProfile".substring(HEATEDLOAN_PROTOCOL_PREFIX.length()));
    }

}
